package com.cgsrp.cgsrp_baby.service.impl;

import com.cgsrp.cgsrp_baby.dao.ThingPhotoMapper;
import com.cgsrp.cgsrp_baby.dao.ThingTagMapper;
import com.cgsrp.cgsrp_baby.domain.ThingPhoto;
import com.cgsrp.cgsrp_baby.domain.ThingTag;
import com.cgsrp.cgsrp_baby.domain.Vaccine;
import com.cgsrp.utils.MinioUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_baby.dao.ImportThingsMapper;
import com.cgsrp.cgsrp_baby.domain.ImportThings;
import com.cgsrp.cgsrp_baby.service.ImportThingsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ImportThingsServiceImpl implements ImportThingsService{

    @Resource
    private ThingTagMapper thingTagMapper;

    @Resource
    private ThingPhotoMapper thingPhotoMapper;

    @Resource
    private MinioUtils minioUtils;

    @Resource
    private ImportThingsMapper importThingsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return importThingsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ImportThings record) {
        return importThingsMapper.insert(record);
    }

    @Override
    public int insertSelective(ImportThings record) {
        return importThingsMapper.insertSelective(record);
    }

    @Override
    public ImportThings selectByPrimaryKey(Integer id) {
        return importThingsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ImportThings record) {
        return importThingsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ImportThings record) {
        return importThingsMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<ImportThings> list(int[] babyIds, Integer curPage, Integer pageSize) {
        ArrayList<ImportThings> list = new ArrayList<>();
        for (int babyId : babyIds) {
            List<ImportThings> list1=importThingsMapper.list(babyId);
            list.addAll(list1);
        }
        System.out.println("list = " + list);
        list.sort(Comparator.comparingInt(ImportThings::getId));
        List<ImportThings> res;
        if (pageSize<list.size()-(curPage-1)*pageSize) {
            res = list.subList((curPage - 1) * pageSize, (curPage - 1) * pageSize + pageSize);
        }else {
            res = list.subList((curPage - 1) * pageSize, list.size());
        }
        PageInfo<ImportThings> pageInfo = new PageInfo<>(res);
        pageInfo.setTotal(list.size());
        PageHelper.startPage(curPage, pageSize);
        return pageInfo;
    }

    @Override
    public String add(ImportThings importThings) {
        try {
            if (importThings.getPhotos()!=null&&importThings.getTags()!=null) {
                importThingsMapper.insert(importThings);
                for (MultipartFile file : importThings.getPhotos()) {
                    String path = minioUtils.upload(file);
                    ThingPhoto thingPhoto = new ThingPhoto();
                    thingPhoto.setThingId(importThings.getId());
                    thingPhoto.setPhotopath(path);
                    thingPhotoMapper.insert(thingPhoto);
                }
                for (String tag : importThings.getTags()) {
                    ThingTag thingTag = new ThingTag();
                    thingTag.setThingId(importThings.getId());
                    thingTag.setTag(tag);
                    thingTagMapper.insert(thingTag);
                }
                return "添加成功";
            }else {
                return "请检查字段！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "添加失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean del(int id) {
        ImportThings importThings = importThingsMapper.selectByPrimaryKey(id);
        if(importThingsMapper.deleteByPrimaryKey(id)<1){
            return false;
        }
        if (thingPhotoMapper.deleteById(id)<1){
            return false;
        }
        if(thingTagMapper.deleteById(id)<1){
            return false;
        }
        return true;
    }
}
